package com.elong.businesstravel.base.baidusdk.location;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

/**
 * 百度定位SDK产品Demo功能主页
 */
public class BaiduLocation {

	private static BaiduLocation sBaiduLocation = null;
	
	private LocationClient mLocClient;
	private MyLocationListener mMyLocationListener;
	private BDLocationListener mPublicLocationListener;

	private LocationMode mLocationMode;
	private boolean mLocationSequency;
	private int mScanSpan;
	private boolean mIsNeedAddress;
	private String mCoordType;
	private boolean mIsNeedDirection;
	
	public BaiduLocation(Context context) {
		mLocClient = new LocationClient(context);
		mMyLocationListener = new MyLocationListener();
	}
	
	private synchronized static void newInstance(Context context){
		if(null == sBaiduLocation){
			sBaiduLocation = new BaiduLocation(context);
			sBaiduLocation.setLocationParams(LocationMode.Hight_Accuracy, false, 1000, true, "bd09ll", true);
		}
	}
	
	public static BaiduLocation getDefaultBaiduLocation(Context context){
		if(null == sBaiduLocation){
			newInstance(context);
		}
		return sBaiduLocation;
	}
	
	/**
	 * 设置定位监听
	 * @param myLocationListener
	 */
	public void setLocationListener(BDLocationListener myLocationListener){
		mPublicLocationListener = myLocationListener;
	}
	
	/**
	 * 启动定位
	 */
	public void startLocation(){
		if(null != mLocClient){
			mLocClient.registerLocationListener(mMyLocationListener);
			mLocClient.start();
		}
	}
	
	/**
	 * 停止定位
	 */
	public void stopLocation(){
		if(null != mLocClient){
			mLocClient.unRegisterLocationListener(mMyLocationListener);
			mLocClient.stop();
		}
	}

	/**
	 * 单次定位，点击一次定位一次
	 */
	public void requestLocation() {
		if (!mLocationSequency && null != mLocClient && mLocClient.isStarted()) {
			// 单次请求定位
			mLocClient.requestLocation();
		}

	}
	
	/**
	 * 设置定位参数
	 * @param locationMode 定位精度
	 * 	LocationMode.Hight_Accuracy 高精度
	 * 	LocationMode.Battery_Saving 低功耗
	 * 	LocationMode.Device_Sensors 仅用设备
	 * 
	 * @param locationSequency 定位模式及间隔时间
	 * 	true  连续定位
	 * 	false 单次定位
	 * 
	 * @param scanSpan 连续定位时间
	 * 	连续定位的时候设置每次定位的时间
	 * 	单位毫秒，当<1000(1s)时，定时定位无效
	 * 
	 * @param isNeedAddress 地址信息
	 * 	true  使用反地理编码
	 * 	false 不使用反地理编码
	 * 
	 * @param coordType 定位坐标类型
	 * 	gcj02     国测局加密经纬度坐标
	 * 	bd09ll 百度加密经纬度坐标
	 * 	bd09   百度加密墨卡托
	 * 
	 * @param isNeedDirection 是否需要方向
	 * 	true 需要方向
	 * 	false 不需要方向
	 */
	public void setLocationParams(
			LocationMode locationMode, boolean locationSequency, 
			int scanSpan, boolean isNeedAddress, 
			String coordType, boolean isNeedDirection){
		
		mLocationMode = locationMode;
		mLocationSequency = locationSequency;
		mScanSpan = scanSpan; 
		mIsNeedAddress = isNeedAddress;
		mCoordType = coordType;
		mIsNeedDirection = isNeedDirection;
		
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(mLocationMode);
		option.setCoorType(mCoordType);
		option.setScanSpan(mScanSpan);
		option.setNeedDeviceDirect(mIsNeedDirection);
		option.setIsNeedAddress(mIsNeedAddress);
		mLocClient.setLocOption(option);
	}

	/**
	 * 实现实位回调监听
	 */
	private class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			// Receive Location
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation) {
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
				sb.append("\ndirection : ");
				sb.append(location.getDirection());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				// 运营商信息
				sb.append("\noperationers : ");
				sb.append(location.getOperators());
			}
			// logMsg(sb.toString());
			Log.i("BaiduLocationApiDem", sb.toString());
			
			if(null != mPublicLocationListener){
				mPublicLocationListener.onReceiveLocation(location);
			}
		}

		@Override
		public void onReceivePoi(BDLocation arg0) {
			if(null != mPublicLocationListener){
				mPublicLocationListener.onReceivePoi(arg0);
			}
		}
	}

}
